package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.user.UpdateUserDto;
import com.opennews.openplatform.dto.user.UpdateUserPasswordDto;
import com.opennews.openplatform.dto.user.UpdateUserRolesDto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.security.MyClaims;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {
    private final UserService userService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/query-details")
    public ResponseEntity<ApiResponse<Map<String, Object>>> queryDetails(@RequestParam String id) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        Map<String, Object> result = userService.queryDetails(accountGroupId, id);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/query-users-not-in-account-group")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryUsersNotInAccountGroup(@RequestParam String keyword, @RequestParam int pageSize, @RequestParam int pageIndex) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        List<Map<String, Object>> result = userService.queryUsersNotInAccountGroup(accountGroupId, keyword, pageSize, pageIndex);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/query-account-group-users")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryAccountGroupUsers(@RequestParam int pageSize, @RequestParam int pageIndex) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        List<Map<String, Object>> result = userService.queryAccountGroupUsers(accountGroupId, pageSize, pageIndex);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/update-current-user-password")
    public ResponseEntity<ApiResponse<Boolean>> updateCurrentUserPassword(@Valid @RequestBody UpdateUserPasswordDto data) {
        MyClaims claims = requestIdentityExtractor.getClaims();

        // Checks if the current password is correct. Otherwise, the password will not be updated.
        boolean isPasswordMatched = userService.validatePassword(claims.username, data.getCurrentPassword());

        // If the current password matches then update to the new password.
        if (isPasswordMatched) {
            userService.updatePassword(claims.accountGroupId, claims.username, data.getNewPassword());
        }

        return httpResponseHelper.getSuccessResponse(isPasswordMatched);
    }

    @PostMapping("/update-current-user")
    public ResponseEntity<ApiResponse<Boolean>> updateCurrentUser(@Valid @RequestBody UpdateUserDto data) {
        String username = requestIdentityExtractor.getClaims().username;
        userService.updateCurrentUser(username, data);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/update-roles")
    public ResponseEntity<ApiResponse<Boolean>> updateRoles(@Valid @RequestBody UpdateUserRolesDto data) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        userService.updateRoles(accountGroupId, data);

        return httpResponseHelper.getSuccessResponse();
    }
}
